package app;

import java.util.*;

import utils.*;

import com.StarMicronics.StarIOSDK.R;

import android.content.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class E_invoce {
	private ListView table;
	private EditText email_address;
	private MyAdapter adapter;
	public View view;
	private Vector<Dictionary<String, String>> data;
	private long ticketID;

	public E_invoce(long param) {
		ticketID = param;
		data = new Vector<Dictionary<String, String>>();
		getData();
		loadXml();
	}
	
	private void getData() {
		TicketList list = AppDelegate.sharedInstance().getMain().ticketsDataSource;
		Dictionary<String, Object> custom = list.memoryObject.get(Long.valueOf(ticketID));
		if (custom != null) {
			Vector<String> custom_emails = (Vector<String>) custom.get(TicketList.CUSTOM_EMAILS);
			Vector<String> emails = (Vector<String>) custom.get(TicketList.EMAILS);
			
			data.removeAllElements();
			if (emails != null) {
				for (int i = 0; i < emails.size(); i++) {
					Dictionary<String, String> temp = new Hashtable<String, String>();
					temp.put("email", emails.elementAt(i));
					data.add(temp);
				}
			}
			if (custom_emails != null) {
				for (int i = 0; i < custom_emails.size(); i++) {
					Dictionary<String, String> temp = new Hashtable<String, String>();
					temp.put("email", custom_emails.elementAt(i));
					temp.put("changable", "1");
					data.add(temp);
				}
			}
		}
	}
	
	private void send() {
		AppDelegate.sharedInstance().getMain().submitWithDeleteTicket(false);
	}
	private void add_email() {
		String testUrl = email_address.getText().toString();
		if (testUrl != null && testUrl.length() > 0) {
			TicketList list = AppDelegate.sharedInstance().getMain().ticketsDataSource;
			Dictionary<String, Object> custom = list.memoryObject.get(Long.valueOf(ticketID));
			if (custom != null) {
				Vector<String> custom_emails = (Vector<String>) custom.get(TicketList.CUSTOM_EMAILS);
				if (custom_emails == null) {
					custom_emails = new Vector<String>();
					custom.put(TicketList.CUSTOM_EMAILS, custom_emails);
				}
				
				Dictionary<String, String> e = new Hashtable<String, String>();
				e.put("email", testUrl);
				e.put("changable", "1");
				custom_emails.add(testUrl);
				data.add(e);
				adapter.notifyDataSetInvalidated();
			}
		}
	}
	
	private final class ViewHolder {  
        private TextView urlText;
        private Button cancelButton;
    }
	
	private final class MyAdapter extends BaseAdapter {  
        private LayoutInflater mInflater;  
        public MyAdapter(Context context) {  
            this.mInflater = LayoutInflater.from(context);  
        }  
        public int getCount() {  
            // TODO Auto-generated method stub  
            return data.size();
        }  
        public Object getItem(int arg0) {  
            // TODO Auto-generated method stub  
            return data.elementAt(arg0);  
        }  
        public long getItemId(int arg0) {  
            // TODO Auto-generated method stub  
            return arg0;  
        }  
        public View getView(int position, View convertView, ViewGroup parent) {  
            ViewHolder holder = null;  
            if (convertView == null) {  
                holder = new ViewHolder();  
                convertView = mInflater.inflate(R.layout.email_cell, null);  
                holder.urlText = (TextView) convertView.findViewById(R.id.email_cell_server);  
                holder.cancelButton = (Button) convertView.findViewById(R.id.email_cell_delete);
                holder.cancelButton.setOnClickListener(new OnClickListener(){
                	public void onClick(View v) {
                		delete();
                	}
                });
                convertView.setTag(holder);
            } else {  
                holder = (ViewHolder) convertView.getTag();  
            }
            
            holder.cancelButton.setText(FilePack.getStringByID(STRING_ID.STR_DELETE));
            
            Dictionary<String, String> dic = data.elementAt(position);
            holder.urlText.setText(dic.get("email"));
            holder.cancelButton.setVisibility(position == selectItem?View.VISIBLE:View.INVISIBLE);
            if (dic.get("changable") == null) {
            	holder.cancelButton.setVisibility(View.INVISIBLE);
            }
            return convertView;  
        }
        private void delete() {
        	TicketList list = AppDelegate.sharedInstance().getMain().ticketsDataSource;
			Dictionary<String, Object> custom = list.memoryObject.get(Long.valueOf(ticketID));
        	if (custom != null) {
    			Vector<String> custom_emails = (Vector<String>) custom.get(TicketList.CUSTOM_EMAILS);
    			Vector<String> emails = (Vector<String>) custom.get(TicketList.EMAILS);
    			
    			int count = 0;
    			if (emails != null) count = emails.size();
    			custom_emails.removeElementAt(selectItem-count);
    			data.remove(selectItem);
            	selectItem = -1;
            	this.notifyDataSetInvalidated();
        	}
        }

        public  void setSelectItem(int selectItem) {  
             this.selectItem = selectItem;  
        }  
        private int  selectItem=-1;  
    }
	
	private Button add_email;
	private Button button_send;
	
	private void loadXml() {
		view = AppDelegate.sharedInstance().activity.getLayoutInflater().inflate(R.layout.e_invoce, null);
		table = (ListView) view.findViewById(R.id.table_emails);
		email_address = (EditText) view.findViewById(R.id.email_address);
		button_send = (Button) view.findViewById(R.id.button_send);
		add_email = (Button) view.findViewById(R.id.add_email);
				
		button_send.setOnClickListener(new OnClickListener(){
        	public void onClick(View v)
        	{
        		send();
        	}
		});
		add_email.setOnClickListener(new OnClickListener(){
        	public void onClick(View v) {
        		add_email();
        	}
		});

		adapter = new MyAdapter(AppDelegate.sharedInstance().activity);
		table.setAdapter(adapter);
		table.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
	        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {  
	              
	            adapter.setSelectItem(arg2);  
	            adapter.notifyDataSetInvalidated();  
	        }
		});
		
		email_address.setHint(FilePack.getStringByID(STRING_ID.STR_EMAIL_ADDRESS));
		button_send.setText(FilePack.getStringByID(STRING_ID.STR_SEND));
		add_email.setText(FilePack.getStringByID(STRING_ID.STR_ADD_EMAIL));
	}
}
